
import datasource

FORMAT_DAY = "%Y%m%d"
SELL_CODE = -1000000
BUY_CODE = 1000000

# 导入函数库
def double_macd(ts_code, n1=5, n2=10):
    # 获取股票的收盘价
    daily_price = datasource.daily(ts_code)
    # 取得过去 ma_n1 天的平均价格
    ma_n1_yesterday = daily_price['close'][1:n1+1].mean()
    ma_n1_today = daily_price['close'][:n1].mean()
    # 取得过去 ma_n2 天的平均价格
    ma_n2_yesterday = daily_price['close'][1:n2+1].mean()
    ma_n2_today = daily_price['close'][:n2].mean()

    # 判断是金叉还是死叉
    if ma_n2_yesterday > ma_n1_yesterday and ma_n2_today < ma_n1_today:
        return BUY_CODE
    elif ma_n2_yesterday < ma_n1_yesterday and ma_n2_today > ma_n1_today:
        return SELL_CODE
    else:
        return round((ma_n2_today - ma_n1_today)/ma_n2_today, 4)

